package reader;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.shhic.itsed.mws.deviceintf.AntennaProtocol;
import com.shhic.itsed.mws.deviceintf.RawTagBank;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.shhic.itsed.mws.deviceintf.ReaderCommandType;
import com.shhic.itsed.mws.deviceintf.ReaderLinkStatusEnum;
import com.tjkcht.midware.reader.Rd30Manager;

@RunWith(Parameterized.class)
public class ReadManagerMulti {
	
	public ReadManagerMulti(RawTagBank bank,int offset,int length){
		this.bank = bank;
		this.offset = offset;
		this.length = length;
		
	}
	
	private RawTagBank bank;
	private int offset;
	private int length;
	private Rd30Manager rd;
	private String readerIp1,readerIp2;
	private int port1,port2;
	public ResultCallBack callBack = new ResultCallBack();

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(
                new Object[][]{
                        {RawTagBank.EPCBANK,0, 1}
//                        {RawTagBank.EPCBANK,0, 10},
//                        {RawTagBank.EPCBANK,0, 33},
//                       
//                     
//                        {RawTagBank.RESERVED,0, 2},
//                        {RawTagBank.RESERVED,1, 10},
//                        {RawTagBank.RESERVED,1, 20},
//                      
//                        
//                        {RawTagBank.TID,0, 4},
//                        {RawTagBank.TID,0, 10},
//                        {RawTagBank.TID,0, 20},
//                    
//                        {RawTagBank.USER,0, 4}, 
//                        {RawTagBank.USER,0, 10},
//                        {RawTagBank.USER,0, 20}

                }

        );
    }

	
	@Before
	public void  setUp() throws Exception {
		 rd = new Rd30Manager();
		 readerIp1 = "192.168.11.171";
		 readerIp2 = "192.168.11.172";
		 port1 = 5084;	
		 port2 = 5085;
		 
		 
		 System.out.println("in setUp!");
	}
	
	@After
	public void tearDown() throws Exception {
		
		rd.close();
		System.out.println("in tearDown!");
	}
	
	private ReaderCommand getCmd(String ip,int port,int ann,RawTagBank bank,int offset,int length){
		ReaderCommand cmd = new ReaderCommand();
		cmd.setReaderIp(ip);
		cmd.setReaderPort(port);
		cmd.setReaderCommandType(ReaderCommandType.SINGLE);
		Set<AntennaProtocol> antennaProtocolSet=new HashSet<AntennaProtocol>();
		AntennaProtocol ap=new AntennaProtocol();
		ap.setAntennaNo(ann);
		antennaProtocolSet.add(ap);
		Set<String> plSet=new HashSet<String>();
		plSet.add("EPC");
		ap.setProtocols(plSet);			
		cmd.setAntennaProtocolSet(antennaProtocolSet);
		List<RawTagField> readTagFieldList=new ArrayList<RawTagField>();
		RawTagField field=new RawTagField();
		field.setBank(bank);
		field.setOffset(offset);
		field.setLength(length);			
		readTagFieldList.add(field);
		cmd.setReadTagFieldList(readTagFieldList);
		return cmd;
	}


	
	@Test
	public void testCmd() throws InterruptedException{
		rd.initReader(readerIp1, port1);
		rd.initReader(readerIp2, port2);
		System.out.println("after initReader");

		ReaderCommand cmd1 = getCmd(readerIp1,port1,1,bank,offset,length);
		ReaderCommand cmd2 = getCmd(readerIp1,port2,1,bank,offset,length);
		
		
		rd.dispatchReadCommand(readerIp1, cmd1, 1000, callBack);
		rd.dispatchReadCommand(readerIp2, cmd2, 1000, callBack);
		Thread.sleep(1000000);
		rd.stopReaderCommand(readerIp1);
		rd.stopReaderCommand(readerIp2);
			
		
		
	}

}
